<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="pic" width="95">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" width="60" height="60" class="head_pic" :onerror="errorImg">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="Author" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Pageviews" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.pageviews }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="Display_time" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.display_time }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getList } from '@/api/table'
import defaultPic from '@/assets/default.png'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      errorImg: 'this.src="' + defaultPic + '"'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        console.log(response)
        debugger
        this.list = response.data.items
        let idx = 0
        this.list.forEach((i) => {
          i['title'] = i['name'] + '西瓜'
          i['author'] = i['type']
          i['display_time'] = '大树'
          i['pageviews'] = '左右'
          idx++
          if (idx === 1) {
            i['pic'] = 'https://img.alicdn.com/bao/uploaded/i3/1714128138/O1CN01RUlntD29zFmZEVyDi_!!1714128138-2-lubanu-s.png'
          } else if (idx === 2) {
            i['pic'] = 'https://img.alicdn.com/bao/uploaded/i3/2908678584/O1CN01GRoA7P2DHWT5wxZfS_!!2908678584.jpg'
          } else if (idx === 3) {
            i['pic'] = 'https://img.alicdn.com/bao/uploaded/i1/693003403/O1CN01w2rC4n1b0cQtAesxd_!!693003403.jpg'
          } else {
            i['pic'] = defaultPic
          }
        })
        this.listLoading = false
      })
    }
  }
}
</script>
